package me.lc.rank;

import java.util.ArrayList;
import java.util.List;

import me.lc.FunPeaceActivity;
import android.content.SharedPreferences;

public class RankHelper {
	private static final int RankType = 1;
	private static final String PREFS_NAME_Rank = "Rank";
	private static final String Local = "local_";
	private static final String Top = "top_";
	private static final String Nearby = "nearby_";
	private static final String No = "no_";
	private static final String Name = "name_";
	private static final String Score = "score_";
	private static final String IsSelf = "isSelf_";
	private static final int MaxRankCount = 10;
	private static List<RankInfo> top10Rank = null;
	private static List<RankInfo> nearbyRank = null;
	private static List<RankInfo> localRank = null;

	/**
	 * 获得在本地排行中与上一名的分数差距，返回0则表示已经是第一名，返回－1表示没数据
	 * 
	 * @return
	 */
	public static int getNextRankScoreDiff(int score) {
		List<RankInfo> ranks = getLocalRank();
		int index = -1;
		for (int i = 0; i < ranks.size(); i++) {
			if (ranks.get(i).getScore() <= score)
				break;
			index = i;
		}
		if (index == -1)
			return 0;
		else
			return ranks.get(index).getScore() - score;
	}

	public static void submitScore(String name, int score) {
		// 保存用户使用过的名字
		// UserInfo user = DC.getUser();
		// user.setLastRankName(name);
		// DC.saveUser(user);

		// 保存本地分数
		List<RankInfo> localRanks = getLocalRank();
		int index = -1;
		for (int i = 0; i < localRanks.size(); i++) {
			if (score > localRanks.get(i).getScore()) {
				index = i;
				break;
			}
		}
		if (index != -1) {
			RankInfo info = new RankInfo();
			info.setNo(index + 1);
			info.setName(name);
			info.setScore(score);
			info.setSelf(false);
			localRanks.add(index, info);
			if (localRanks.size() > MaxRankCount) {
				localRanks.remove(MaxRankCount);
			}
			// 更新后续的排名
			for (int i = index + 1; i < localRanks.size(); i++) {
				localRanks.get(i).setNo(i + 1);
			}
			saveRank(Local, localRanks);
		}
		// 提交服务器
		SAEHelper.submitScore("dddd", name, RankType, score);
	}

	public static List<RankInfo> getLocalRank() {
		if (localRank == null) {
			localRank = getRank(Local);
		}
		return localRank;
	}

	public static List<RankInfo> getTop10Rank() {
		if (top10Rank == null) {
			top10Rank = getRank(Top);
		}
		return top10Rank;
	}

	public static void syncTop10Rank() {
		SAEHelper.getTop10Rank(RankType);
	}

	public static void updateNearbyRank(List<RankInfo> ranks) {
		nearbyRank = ranks;
		saveRank(Nearby, ranks);
	}

	public static List<RankInfo> getNearbyRank() {
		if (nearbyRank == null) {
			nearbyRank = getRank(Nearby);
		}
		return nearbyRank;
	}

	public static void syncNearbyRank() {
		// SAEHelper.getNearby10Rank(RankType);
	}

	public static void updateTop10Rank(List<RankInfo> ranks) {
		top10Rank = ranks;
		saveRank(Top, ranks);
	}

	private static List<RankInfo> getRank(String type) {
		List<RankInfo> ranks = new ArrayList<RankInfo>();
		SharedPreferences settings = FunPeaceActivity.Instance
				.getSharedPreferences(PREFS_NAME_Rank, 0);

		for (int i = 1; i <= MaxRankCount; i++) {
			RankInfo rankInfo = new RankInfo();
			int no = settings.getInt(type + No + i, -1);
			if (no != -1) {
				rankInfo.setNo(no);
				rankInfo.setName(settings.getString(type + Name + i, null));
				rankInfo.setScore(settings.getInt(type + Score + i, 0));
				rankInfo.setSelf(settings.getBoolean(type + IsSelf + i, false));
			} else {
				rankInfo.setNo(i);
				rankInfo.setName(null);
				rankInfo.setSelf(false);
			}
			ranks.add(rankInfo);
		}
		return ranks;
	}

	private static void saveRank(String type, List<RankInfo> ranks) {
		if (ranks != null) {
			SharedPreferences settings = FunPeaceActivity.Instance
					.getSharedPreferences(PREFS_NAME_Rank, 0);
			SharedPreferences.Editor editor = settings.edit();

			int i = 1;
			for (RankInfo rankInfo : ranks) {
				editor.putInt(type + No + i, rankInfo.getNo());
				editor.putString(type + Name + i, rankInfo.getName());
				editor.putInt(type + Score + i, rankInfo.getScore());
				editor.putBoolean(type + IsSelf + i, rankInfo.isSelf());
				i++;
			}

			editor.commit();
		}
	}
}
